package com.zqc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zqc.common.Const;
import com.zqc.common.R;
import com.zqc.entity.ShoppingCart;
import com.zqc.service.ShoppingCartService;
import com.zqc.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    public ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart) {
        // 设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 判断当前菜品或套餐是否存在，存在，数量加一，不存在，直接保存，数量为1
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>();
        queryWrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        // 判断添加菜品还是套餐
        if (dishId != null) {
            // 添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        // 判断购物车是否存在
        if (cart != null) {
            // 数量加一
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        } else {
            // 添加购物车
            shoppingCart.setNumber(Const.CART_DEFAULT_NUMBER);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cart = shoppingCart;
        }

        return R.success(cart);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        // 查询该菜品或套餐
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        // 判断添加菜品还是套餐
        if (dishId != null) {
            // 减少的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            // 减少的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 查询
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if (cart != null) {
            // 判断数量
            Integer number = cart.getNumber();
            if (number > 1) {
                // 让数量减一
                cart.setNumber(number - 1);
                shoppingCartService.updateById(cart);
            } else {
                // 清空该菜品或订单
                shoppingCartService.removeById(cart.getId());
                cart.setNumber(0);
            }
        }

        return R.success(cart);

    }

    /**
     * 查看购物车列表
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("userId={}",BaseContext.getCurrentId());
        List<ShoppingCart> cartList = shoppingCartService.list(
                new LambdaQueryWrapper<ShoppingCart>()
                        .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                        .orderByAsc(ShoppingCart::getCreateTime));

        return R.success(cartList);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId()));
        return R.success("清空购物车成功");
    }
}
